<?php use_stylesheets_for_form($form) ?>
<?php use_javascripts_for_form($form) ?>

<!-- Load the queue CSS -->
<style type="text/css">@import url(/js/plupload/jquery.ui.plupload/css/jquery.ui.plupload.css);</style>

<!-- Third party script for BrowserPlus runtime (Google Gears included in Gears runtime now) -->
<script type="text/javascript" src="http://bp.yahooapis.com/2.4.21/browserplus-min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>

<!-- Load plupload and all it's runtimes and finally the jQuery queue widget -->
<script type="text/javascript" src="/js/plupload/plupload.full.js"></script>
<script type="text/javascript" src="/js/plupload/jquery.ui.plupload/jquery.ui.plupload.js"></script>

<form action="<?php echo url_for('servicios/'.($form->getObject()->isNew() ? 'create' : 'update').(!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : '')) ?>" method="post" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>
<?php if (!$form->getObject()->isNew()): ?>
<input type="hidden" name="sf_method" value="put" />
<?php endif; ?>
<table width="100%" border="0" cellpadding="0" cellspacing="0" >
    <tr>
       <td align="center">
            
            
        
 <table class="tableTituloForm" style="" border="0" cellpadding="0" cellspacing="0" >
    <tr>
        
        <td class="columnaTituloForm" align="left">DATOS</td>
        <td style="background: #488eb8" align="right">
            <input type="submit" value="Guardar" id="submitId" class="button-done submitId" style="float: right;">
            
            
            <?php if (!$form->getObject()->isNew()): ?>
            <?php echo link_to('Delete', 'servicios/delete?id='.$form->getObject()->getId(), array('method' => 'delete', 'style' => 'float: right; background: #d05848; border: #d05848;','class' => 'button-done submitId','confirm' => 'Are you sure?')) ?>
            <?php endif; ?>
            
            <a class="button-done submitId" style="float: right;" href="<?php echo url_for('servicios/index?servicio_tipo='.$servicio_tipo) ?>">Back to list</a>
            
            
            
        </td>
        
        
    </tr>
    <tr>
        <td class="columnaFilaForm" align="left" colspan="2">
            <font class="camposIngreso<?php if($form['nombre']->renderError()) echo "Error"; ?>">
                Nombre <?php echo $form['nombre'] ?>
            </font>
        </td>
        
        
    </tr>
     <tr>
       
        
        <td class="columnaFilaForm" align="left">
            <font class="camposIngreso<?php if($form['categoria_list']->renderError()) echo "Error"; ?>">
                Categoria <?php echo $form['categoria_list'] ?>
            </font>
        </td>
        
        <td class="columnaFilaForm" align="left">
            <?php if($servicio_tipo==1){ ?>
            <font class="camposIngreso<?php if($form['marca_id']->renderError()) echo "Error"; ?>">
                Marca <?php echo $form['marca_id'] ?>
            </font>
            <?php } ?>
        </td>
        
        
    </tr>
    <tr>
        <td class="columnaFilaForm" align="left" valign="top" colspan="2">
            <div  class="camposIngreso<?php if($form['mancheta']->renderError()) echo "Error"; ?>">
                Descripcion
                <div class="clear" style="margin-bottom: 10px;"></div>
                <?php echo $form['mancheta'] ?>
                
            </div>
        </td>
    </tr>
    
    <tr>
        <td class="columnaFilaFormTitulo" align="left" >
           Detalle de los precios
        </td>
        <td class="columnaFilaFormTitulo" align="right" valign="top">
            <?php if($servicio_tipo==1){ ?>
           <button id="add_precios" type="button"><?php echo "Añadir Precio al "?><?php echo ucwords($tipo); ?></button>
           <?php }?>
        </td>
    </tr>
    
    <tr>
        <td class="columnaFilaForm" align="left" valign="top" colspan="2">
            <div  class="camposIngreso">
               
            <div id="extraprecios">
                    <table border="0" style=" text-align: center;">
                        <tr style="border-bottom: 1px solid #ccc;">
                            <?php if (!$form->getObject()->isNew()){ ?>
                            <td width="50px" >Interno</td>
                            <?php }?>
                            <td width="150px" >Cod</td>
                            <?php if($servicio_tipo==2){ ?>
                            <td width="150px" >Tamaño Mascota</td>
                            <?php }else{?>
                            <td width="200px">Presentacion</td>
                            <?php }?>
                            <td width="170px" style="padding-bottom: 5px;">Precio</td>
                            <td width="150px">Pto.-</td>
                            <td width="150px">Pto.+</td>
                            <td width="70px" align="center">IVA</td>
                            <?php if($servicio_tipo==1){ ?>
                            <td width="50px"></td>
                            <?php }?>
                        </tr>
                    
                    </table>
                    <?php 
                    
                    for($i=0;$i<count($form['precios']);$i++){ ?>
                    <div id="precios<?php echo $i ?>"> 
                        <table border="0"  style="text-align: center;">
                        <tr style="border-bottom: 1px solid #ccc;">
                            <?php if (!$form->getObject()->isNew()){ ?>
                            <td width="50px"><?php echo $form['precios'][$i]['id']->getValue(); ?></td>
                            <?php }?>
                            <td width="150px"><?php echo $form['precios'][$i]['codigo']; ?></td>
                            <?php if($servicio_tipo==2){ ?>
                            <td width="150px"><?php echo $form['precios'][$i]['tamano_id']; ?></td>
                            <?php }else{?>
                            <td width="200px"><?php echo $form['precios'][$i]['presentacion']; ?></td>
                            <?php }?>
                            <td width="150px" style="padding-bottom: 5px;padding-top: 5px" class="camposIngreso<?php if($form['precios'][$i]['precio']->renderError()) echo "Error"; ?>">
                                <?php echo $form['precios'][$i]['precio']; ?></td>
                            <td width="150px"><?php echo $form['precios'][$i]['puntos_costo']; ?></td>
                            <td width="150px"><?php echo $form['precios'][$i]['puntos_regalo']; ?></td>
                            <td width="50px"><?php echo $form['precios'][$i]['is_impuesto']; ?></td>
                            <?php if($servicio_tipo==1){?>
                            <td width="50px" align="center">
                                <?php if($i>0){?>
                                <button id="dele_precios<?php echo $i ?>" type="button"><?php echo "Quitar"?></button>
                                
                                <script type="text/javascript">
                                        $('#dele_precios<?php echo $i ?>').click(function() {

                                            $.ajax({
                                                url: '<?php echo url_for('servicios/delPre')?>?id=<?php echo $form['precios'][$i]['id']->getValue(); ?>&servicio_tipo=<?php echo $servicio_tipo; ?>',
                                                dataType: "script",
                                                type: "POST",
                                                success: function(theResponse){                
                                                    
                                                    $("#extraprecios").html(theResponse);

                                                }});
                                            
                                        });
                                 </script>
                                 <?php }?>
                            </td>
                            <?php }?>
                                    
                             <div style="visibility: hidden; position: absolute;">
                                <?php echo $form['precios'][$i]['precio_etiqueta_id']; ?>
                                <?php echo $form['precios'][$i]['empresa_id']; ?>
                            </div>   
                                
                            
                                
                            
                        </tr>
                      
                        </table>
                        
                    </div>

                    
                    <?php } ?>
                    
                </div>
                
                
            </div>
        </td>
    </tr>
    
    <tr>
        <td class="columnaFilaForm" align="left" valign="top" colspan="2">
            <div  class="camposIngreso<?php if($form['contenido']->renderError()) echo "Error"; ?>">
                Descripcion completa
                <div class="clear" style="margin-bottom: 10px;"></div>
                <?php echo $form['contenido'] ?>
                
            </div>
        </td>
    </tr>
    
    <tr>
        <td class="columnaFilaForm" align="left" colspan="2">
            <div class="camposIngreso<?php if($form['producto_status_list']->renderError()) echo "Error"; ?>">
                Asignacion de Status
                    <div class="clear" style="margin-bottom: 10px; border-bottom: 1px dashed #ccc"></div>
                    <?php echo $form['producto_status_list'] ?>
            </div>
            
            
            <div class="clear"></div>
 </td>
    </tr>
<?php if (!$form->getObject()->isNew()){ ?>
    <tr>
        <td class="columnaFilaForm" align="left" colspan="2">

    
               
 
   <?php 
   $galeria = $form->getObject();
   $archivos = $galeria->getArchivo();
   $counter=1;
   foreach ($archivos as $archivo){ 
       
       ?>
        <div  id="imagen<?php echo $archivo->getId(); ?>" style="float:left; margin-right:10px; margin-bottom:10px;">

      
            <table border="0" >
                <tr>
                    <td >
         <?php
       
                echo '<img alt="'.$archivo->getNombre().'" width="195" height="195" border="0" src="http://call.sisapp.us/uploads/productos/'.$archivo->getNombre().'" >';
        ?>
                    </td>
                    <td class="">
                        <img onclick="<?php echo "removeImagen(".$archivo->getId().")"; ?>" src="/images/frontend/producto/delete.png" alt="Eliminar Foto de Galería" title="Eliminar Foto de Galería"/>
                    </td>
                </tr>
                
            </table>
        </div>
        <?php        } ?>

        </td></tr>
  <?php } ?>
    <tr>
        <td class="columnaFilaForm" align="center" colspan="2">

    
 
                
     <div id="uploader" style="width: 1000px;" >
     <p>Tu Navegador no soporta el plugin.</p>
     </div>
     

        </td>
    </tr>
        
    
    
    
    
    </tr>
    
    
      
      <tr>
        <td colspan="2">
          <?php echo $form->renderGlobalErrors() ?>
          <?php echo $form->renderHiddenFields(false) ?>
          
          
        </td>
      </tr>
      
  </table>
            
            </td>
    </tr>
</table>


</form>

<script type="text/javascript">
   


var prec = <?php print_r($form['precios']->count())?>;


function addPre(num) {
  var r = $.ajax({
    type: 'GET',
    url: '<?php echo url_for('servicios/addPreForm')?>'+'<?php echo   ($form->getObject()->isNew()?'':'?id='.$form->getObject()->getId()).($form->getObject()->isNew()?'?num=':'&num=')?>'+num+'&servicio_tipo=<?php echo $servicio_tipo; ?>',
    async: false
  }).responseText;
  return r;
}



  $('button#add_precios').click(function() {
    $("#extraprecios").append(addPre(prec));
    prec = prec + 1;
  });



    function removeImagen(Id){
        $("#imagen"+Id).remove();
        var r = $.ajax({
            type: 'GET',
            url: '<?php echo url_for('servicios/deleteImagen')?>'+'?id='+Id,
            async: false
        }).responseText;
        
    }
   
// Convert divs to queue widgets when the DOM is ready
$(function() {
	$("#uploader").plupload({
		// General settings
		runtimes : 'gears,flash,silverlight,browserplus,html5',
		url : '<?php echo url_for('servicios/upload') ?>',
		max_file_size : '10mb',
//		chunk_size : '1mb',
		unique_names : true,

		// Resize images on clientside if we can
		resize : {width : 350, height : 350, quality : 90},

		// Specify what files to browse for
		filters : [
			{title : "Image files", extensions : "JPG,jpg,GIF,gif,PNG,png"},
			{title : "Zip files", extensions : "zip"}
		],

		// Flash settings
		flash_swf_url : '/js/plupload/plupload.flash.swf',

		// Silverlight settings
		silverlight_xap_url : '/js/plupload/plupload.silverlight.xap'
	});

	// Client side form validation
	$('form').submit(function(e) {
        
        var uploader = $('#uploader').plupload('getUploader');
        // Files in queue upload them first
        if (uploader.files.length > 0) {
            // When all files are uploaded submit form
            uploader.bind('StateChanged', function() {
                if (uploader.files.length === (uploader.total.uploaded + uploader.total.failed)) {
                   
                    $('form')[0].submit();
                }
            });
                
            uploader.start();
        } else
                    $('form')[0].submit();

        return false;
    });
    
});

</script>